var dataErorrs = {
    'first_name': 'Please enter your first name.',
    'last_name': 'Please enter your last name.',
    'email': 'Please enter a valid email address.',
    'password': 'Please enter your password.',
    'password2': 'Please enter your password again.',
    'phone': 'Please enter your phone number, only digits.',
    'country': 'Please choice your contry.',
    'agent': '',
    'certificate': 'You have read and accepted our Services Agreement.'
};

//First name must contain only letters, numbers and underscores!
//Last name must contain only letters, numbers and underscores!

function signUpNewUser() {
    var isFillData = {'first_name': false, 'last_name': false, 'email': false, 'password': false, 'password2': false, 'phone': false, 'country': false, 'agent': true, 'certificate': false};
    var dataErrorsCopy = dataErorrs;
    var data = {
        'first_name': $('#sign_up_first_name').val(),
        'last_name': $('#sign_up_last_name').val(),
        'email': $('#sign_up_email').val(),
        'password': $('#sign_up_password').val(),
        'password2': $('#sign_up_verify_password').val(),
        'phone': $('#sign_up_phone').val(),
        'country': $('#sign_up_country').val(),
        'agent': $('#sign_up_agent').is(':checked'),
        'language': languageType
    };

    var isNotHaveErrorsCounter = 0;

    if (isValidFirstName(data.first_name)) {
        isFillData.first_name = true;
        isNotHaveErrorsCounter = isNotHaveErrorsCounter + 1;
    }

    if (isValidLastName(data.last_name)) {
        isFillData.last_name = true;
        isNotHaveErrorsCounter = isNotHaveErrorsCounter + 1;
    }

    if (isValidEmailAddress(data.email)) {
        isFillData.email = true;
        isNotHaveErrorsCounter = isNotHaveErrorsCounter + 1;
    }

    if (isValidPassword(data.password, data.password2)) {
        isFillData.password = true;
        isFillData.password2 = true;
        isNotHaveErrorsCounter = isNotHaveErrorsCounter + 1;
    }

    if (isValidPhone(data.phone)) {
        isFillData.phone = true;
        isNotHaveErrorsCounter = isNotHaveErrorsCounter + 1;
    }

    if (data.country > 0) {
        isFillData.country = true;
        isNotHaveErrorsCounter = isNotHaveErrorsCounter + 1;
    }

    if ($('#sign_up_certificate').is(':checked')) {
        isFillData.certificate = true;
        isNotHaveErrorsCounter = isNotHaveErrorsCounter + 1;
    }

    if (isNotHaveErrorsCounter === 7) {
        hideSignUpPopup();
        signup(data);
    } else {
        $('.insert').remove();

        var counter = 0;
        $.each(isFillData, function(i, v) {
            if (!v) {
                $('.popup_sign_up div.field').each(function(ii, vv) {
                    if (ii === counter) {
                        $(vv).append("<div class='insert'>" + dataErorrs[i] + "</div>");
                    }
                });
            }
            counter = counter + 1;
        });
        
        isNotHaveErrorsCounter = 0;
        dataErorrs = dataErrorsCopy;
    }
}

function isValidDouble(double) {
    if (double != "") {
        var pattern = new RegExp(/^\d{0,9}(\.\d{0,3}){0,2}$/);
        if (pattern.test(double)) {
            return true;
        }
        
        return false;
    } else {
        return false;
    }
}

function isValidFirstName(name) {
    if (name != "") {
        var pattern = new RegExp(/^\w+$/);
        if (pattern.test(name)) {
            return true;
        }

        dataErorrs.first_name = 'First name must contain only letters, numbers and underscores.';
        return false;
    } else {
        return false;
    }
}

function isValidLastName(name) {
    if (name != "") {
        var pattern = new RegExp(/^\w+$/);
        if (pattern.test(name)) {
            return true;
        }

        dataErorrs.last_name = 'Last name must contain only letters, numbers and underscores.';
        return false;
    } else {
        return false;
    }
}

function isValidPhone(phone) {
    var pattern = new RegExp(/\d/);
    return pattern.test(phone);
}

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
    return pattern.test(emailAddress);
}

function isValidPassword(password1, password2) {
    var pattern = new RegExp(/(?=.*\d)(?=.*[a-z]).{6,}/);
    if (password1 != "") {
        if (!pattern.test(password1)) {
            dataErorrs.password = 'Use a mix of at least 6 letters (A-Z and a-z), numbers.';
            return false;
        }

        if (password1 !== password2) {
            dataErorrs.password2 = 'Your passwords do not match';
            return false;
        }
        
        return true;
    }

    return false;
}


//Use a mix of at least 6 letters (A-Z and a-z), numbers.
//Your passwords do not match.